Thread: [Help} Array of integers keeps returning value 2

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    18

    [Help} Array of integers keeps returning value 2

    Hi all, am currently doing a project from KNKing's C program a modern approach.

    3 files involved. queueADT.h, queueADT.c, stackclient.c

    code for queueADT.h

    Code:
    #ifndef QUEUE_H#define QUEUE_H
    
    
    #include <stdbool.h>  /* C99+ only */
    
    
    typedef int Item;
    typedef struct queue_type *Queue;
    
    
    
    
    Queue create(void);
    void destroy(Queue s);
    void insertend(Queue q, Item i);
    void removebegin(Queue q);
    Item returnfirst(Queue q);
    Item returnlast(Queue q);
    bool isempty(Queue q);
    
    
    #endif
    relevant code oif queueADT.c
    Code:
     
    //structure 
    struct queue_type {
        Item queue[MAXQUEUESIZE];
        int firstemptyslot;
        int toberemoved;
        int numitems;
    };
    
    void insertend(Queue q,Item i)   //inserting number into the next slot in array
    {
        if (q->numitems==100)
            queue_overflow();
        q->queue[q->firstemptyslot]= i;
        q->firstemptyslot++;
        q->numitems++;
        if (q->firstemptyslot>99)
            q->firstemptyslot=0;
    }
    
    Queue create(void)  //creating pointer to structure
    {
      Queue q = malloc(sizeof(struct queue_type));
      if (q == NULL)
        terminate("Error in create: queue could not be created.");
      q->firstemptyslot = 0;
      q->toberemoved = 0;
      q->numitems = 0;
      return q;
    }
    
    Item returnfirst(Queue q){  //returning first number in the array
        if (isempty(q))
            queue_underflow();
        if (q->firstemptyslot == 0)
        return q->queue[q->toberemoved];
    
    
    }

    relevant code of stackclient.c (main). Basically I insert some numbers into the strcuture and get the program to print it back out

    Code:
    #include <stdio.h>
    #include "queueADT.h"
    
    
    int main(void)
    {
      Queue s1, s2;
      Item n;
    
    
      s1 = create();
      s2 = create();
    
    
      insertend(s1, 4);
      insertend(s1, 8);
    
    
    
    
    
    
      printf("First number %d from s1\n", returnfirst(s1));
      removebegin(s1);
      //etc etc continue printing out numbers
    The problem is that my program keeps giving out the number 2, or rather the function that returns a number from the structure in the array keeps returning the number 2, even though I inserted numbers such as 4 or 8 or other numbers.

    I have spent quite some time changing things around but I cant figure out what is wrong. Would appreciate any kind help sirs thank you. Apologies if any of the code is formatted badly because I tried changing some code to fix it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    returnfirst appears to be incomplete: what do you return when the condition of the second if statement evaluates to false?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    18
    Quote Originally Posted by laserlight View Post
    returnfirst appears to be incomplete: what do you return when the condition of the second if statement evaluates to false?
    Hi sir that seems to have been causing the proble. Looks like i forgot to remove that line under return first lol .___.

    Thank you!!!! Solved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-18-2013, 02:21 PM
  2. Replies: 3
    Last Post: 02-23-2013, 11:55 PM
  3. Replies: 3
    Last Post: 05-09-2012, 06:41 AM
  4. Replies: 3
    Last Post: 03-26-2012, 10:50 AM
  5. Replies: 9
    Last Post: 04-07-2010, 10:03 PM

Tags for this Thread